package com.power.using.sharlockchat.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.application.MyApplication;
import com.power.using.sharlockchat.base.BaseBackFragment;
import com.power.using.sharlockchat.bean.QuestionStatus;
import com.power.using.sharlockchat.bean.ShowBean;
import com.power.using.sharlockchat.eventbus.EventCenter;
import com.power.using.sharlockchat.http.HttpApi;
import com.power.using.sharlockchat.http.Request;
import com.power.using.sharlockchat.utils.HttpUtils;
import com.power.using.sharlockchat.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2019/1/21.
 * Time: 2019/1/21  11:57
 */
public class ShowQuestionFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.tv_question_name)
    TextView tvQuestionName;
    @BindView(R.id.tv_question_telphone)
    TextView tvQuestionTelphone;
    @BindView(R.id.tv_question_title)
    TextView tvQuestionTitle;
    @BindView(R.id.tv_question_description)
    TextView tvQuestionDescription;
    Unbinder unbinder;
    @BindView(R.id.bt_solved_status)
    Button btSolvedStatus;
    private ShowBean showBean;

    public static ShowQuestionFragment newInstance(ShowBean showBean) {
        Bundle args = new Bundle();
        args.putSerializable("getshowbean", showBean);
        ShowQuestionFragment fragment = new ShowQuestionFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show_question, container, false);
        unbinder = ButterKnife.bind(this, view);
        intView(view);
        return view;
    }

    private void intView(View view) {
        toolbarTitle.setText("问题展示");

        tvQuestionDescription.setMovementMethod(ScrollingMovementMethod.getInstance());

        Bundle arguments = getArguments();
        if (arguments != null) {
            showBean = (ShowBean) arguments.getSerializable("getshowbean");
        }

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initDate();
    }

    private void initDate() {
        if (showBean != null) {
            tvQuestionTitle.setText(showBean.getTitle());
            tvQuestionDescription.setText(showBean.getDescription());
        }
    }

    @OnClick({R.id.tv_question_telphone, R.id.bt_solved_status})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_question_telphone:
                ToastUtils.showLongToast(getActivity(), "我解答了");
                String phone_number = tvQuestionTelphone.getText().toString();
                //删除字符串首部和尾部的空格
                phone_number = phone_number.trim();

//        if (phone_number != null && !phone_number.equals("")) {
//            //调用系统的拨号服务实现电话拨打功能
//            Intent t = new Intent();
//            // Action:动作.我具体想做什么事
//            // Intent.ACTION_DIAL: 激活拨号界面
//            // Intent.ACTION_CALL: 直接拨打电话
//            t.setAction(Intent.ACTION_DIAL);
//            // Data: 数据.具体的动作所需要的附加数据
//            //封装一个拨打电话的intent，并且将电话号码包装成一个Uri对象传入
//            t.setData(Uri.parse("tel:" + "123456"));
//            startActivity(t);
//        }
                showBean.setStatus("0");
                MyApplication.getShowBeanList().add(showBean);
                EventCenter<ShowBean> eventCenter = new EventCenter<>(1, showBean);

                EventBus.getDefault().post(eventCenter);
                _mActivity.onBackPressed();
                break;
            case R.id.bt_solved_status:
                showLoadingDialog("正在提交,请稍等");
                Map<String,String> parms=new HashMap<>();
                parms.put("question_status","ok");
                HttpUtils.requestPost(_mActivity, HttpApi.QUESTION_STATUS,parms,new Request(){
                    @Override
                    public void success(String json) {
                        super.success(json);

                        QuestionStatus questionStatus = new Gson().fromJson(json, QuestionStatus.class);
                        if(questionStatus.getCode()==200){
                            Log.i("question_status", "success: "+json);
                            btSolvedStatus.setText("已解答");
                            ToastUtils.showLongToast(_mActivity,questionStatus.getData());
                            cancelLoadingDialog();
                            _mActivity.onBackPressed();

                        }

                    }

                    @Override
                    public void error(String error) {
                        super.error(error);
                        Log.i("question_status", "success: "+error);
                        cancelLoadingDialog();
                    }
                });


                break;
        }


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();
    }


}
